package org.example.dao;

import lombok.Getter;
import java.util.HashMap;
import java.util.Iterator;

@Getter
public class ManageClientThreads {
    //得到管理线程的集合的方法
    private static final HashMap<String,ServerConnectClientThread> hm = new HashMap<>();

    //添加线程对象到hm集合
    public static void addClientThread(String username,ServerConnectClientThread serverConnectClientThread){
        hm.put(username,serverConnectClientThread);
    }

    //根据username返回一个线程
    public static ServerConnectClientThread getServerConnectClientThread(String username){
        return hm.get(username);
    }

    //增加一个方法，从集合中移除某个线程对象
    public static void removeServerConnectClientThread (String username){
        hm.remove(username);
    }

    //这里编写方法，返回在线用户列表
    public static String getOnlineUser(){
        //集合遍历，遍历HashMap的key
        Iterator<String> iterator = hm.keySet().iterator();//通过迭代器来获取键值
        StringBuilder onlineUserList = new StringBuilder();
        while (iterator.hasNext()){//检查序列中是否还有元素
            onlineUserList.append(iterator.next()).append(" ");//拼接成字符串进行返回
        }
        return onlineUserList.toString();
    }

    public static HashMap<String, ServerConnectClientThread> getHm() {
        return hm;
    }
}
